
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
// import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:moonplay/common/app_common.dart';
// import 'package:moonplay/router/NavigatorUtil.dart';
import 'package:moonplay/widgets/popup_window.dart';
// import 'package:back_button_interceptor/back_button_interceptor.dart';

Set dict = Set();
bool loadingStatus = false;
dynamic getUrl;
class Loading {
  static dynamic ctx;

  static void before(uri, text) {
    getUrl = uri;
    dict.add(uri); // 放入set变量中
    // 已有弹窗，则不再显示弹窗, dict.length >= 2 保证了有一个执行弹窗即可，
    if (loadingStatus == true || dict.length >= 2) {
      return ;
    }
    loadingStatus = true; // 修改状态

    // 请求前显示弹窗
    var position = RelativeRect.fromLTRB(0, 0.0, 0.0, 0.0);
    showPopupWindow(
      context: ctx,
      fullWidth: true,
      isShowBg: true,   //是否显示背景遮罩
      position: position,
      elevation: 0.0,
      bakFun: _showPopBackFun,    //如果需要在请求的时候点击任意地方关闭请求，则将此句放开
      child: Container(
        width: ScreenUtil().getWidthPx(750),
        height: ScreenUtil().getHeightPx(1334),
        decoration: BoxDecoration(
          color: Colors.transparent,
          borderRadius: BorderRadius.all(Radius.circular(5.0))
        ),
        child: Center(
          child: LoadingView()
        )
      ),
    );
  }

  static void complete(uri) {
    _showPopBackFun(ctx);
  }
}

_showPopBackFun(ctx){
  //通知http取消请求
  // EventBus.instance.commit(EventKeys.CancelHttp,{'data': 4});
  dict.remove(getUrl);
  // 所有接口接口返回并有弹窗
  if (dict.length == 0 && loadingStatus == true) {
    loadingStatus = false; // 修改状态
    // 完成后关闭loading窗口
    Navigator.pop(ctx);
  }
}


class LoadingView extends StatefulWidget {
  @override
  _LoadingViewState createState() => _LoadingViewState();
}

class _LoadingViewState extends State<LoadingView> {
  @override
  void initState() {
    super.initState();
    // BackButtonInterceptor.add(myInterceptor);
  }

  @override
  void dispose() {
    // BackButtonInterceptor.remove(myInterceptor);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      child: SpinKitDualRing(color: Colors.white54,size: 30, lineWidth: 2),
    );
  }

  bool myInterceptor(bool stopDefaultButtonEvent) {
    //通知http取消请求
    // eventBus.fire(CancelHttp(4));
    dict.remove(getUrl);
    // 所有接口接口返回并有弹窗
    if (dict.length == 0 && loadingStatus == true) {
      loadingStatus = false; // 修改状态
    }
  }
}

